package com.boot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.pojo.Headline;
import com.boot.service.HeadlineService;
import com.boot.mapper.HeadlineMapper;
import com.boot.utils.JwtHelper;
import com.boot.utils.Result;
import com.boot.utils.ResultCodeEum;
import com.boot.views.PortalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author liyang
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2025-04-14 13:27:54
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{

    @Autowired
    private  HeadlineMapper headlineMapper;
    @Autowired
    private JwtHelper jwtHelper;


    @Override
    public Result findNewsPage(PortalVo portalVo) {
       IPage<Map> page = new Page<>(portalVo.getPageNum(),portalVo.getPageSize());
       headlineMapper.selectMyPage(page, portalVo);
       List<Map> list = page.getRecords();
       Map<String, Object> map = new HashMap<>();
       map.put("pageData", list);
       map.put("pageNum", page.getCurrent());
       map.put("pageSize", page.getSize());
       map.put("totalPage", page.getTotal());
       map.put("totalSize", page.getTotal());
       Map<String, Object> pageInfo = new HashMap<>();
       pageInfo.put("pageInfo", map);
       return Result.ok(pageInfo);
    }

    @Override
    public Result showHeadlineDetail(Integer id) {
        Map data = headlineMapper.queryDetailMap(id);
        Map<String, Object> map = new HashMap<>();
        map.put("headline", data);
        Headline headline = new Headline();
        headline.setHid(id);
        headline.setVersion((Integer) data.get("version"));
        headline.setPageViews((Integer) data.get("pageViews") + 1);
        headlineMapper.updateById(headline);
        return Result.ok(map);
    }

    @Override
    public Result publish(Headline headline,String token) {
        int userId = jwtHelper.getUserId(token).intValue();
        headline.setPublisher(userId);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        int insertRow = headlineMapper.insert(headline);
        if (insertRow > 0) {
            return  Result.ok(Map.of("id", headline.getHid()));
        }
        return Result.build(null,510, "数据插入失败");
    }

    @Override
    public Result findHeadlineByHid(Integer hid) {
        Headline headline = headlineMapper.selectById(hid);
        return Result.ok(Map.of("headline", headline));
    }

    @Override
    public Result updateByUser(Headline headline, String token) {
        int userId = jwtHelper.getUserId(token).intValue();
        Headline headlineSource = headlineMapper.selectById(headline.getHid());
        if (userId != headlineSource.getPublisher()) {
            return Result.build(null, ResultCodeEum.NOPOWER);
        }
        headline.setVersion(headlineSource.getVersion());
        headline.setUpdateTime(new Date());
        int updateRow = headlineMapper.updateById(headline);
        if (updateRow > 0) {
            return Result.ok(null);
        }
        return Result.build(null,508,"数据更新失败");
    }
}




